package com.zhaofan.offer;

import java.util.LinkedList;
import java.util.List;

public class No34 {
    List<List<Integer>> res=new LinkedList<>( );
    LinkedList<Integer> path=new LinkedList<>();

    public List<List<Integer>> pathSum(TreeNode root, int target) {
        findPath(root,target);
        return res;
    }

    public void findPath(TreeNode node, int sum){
        if (node==null){
            return;
        }
        path.add(node.val);
        sum-=node.val;
        if(sum==0&&node.left==null&&node.right==null ){
            res.add(new LinkedList<>(path));
        }
        findPath(node.left,sum);
        findPath(node.right,sum);
        path.pollLast();
    }


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
